package com.example.malltest.admin.service.impl;

import com.example.malltest.admin.entity.Admin;
import com.example.malltest.admin.entity.AdminDTO;
import com.example.malltest.admin.mapper.AdminMapper;
import com.example.malltest.admin.service.AdminService;
import com.example.malltest.common.ServiceCode;
import com.example.malltest.common.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
 * 业务层
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 登录
     * @param adminDTO
     * @return
     */
    public Admin login(AdminDTO adminDTO){
        Admin admin = adminMapper.getByName(adminDTO.getName());
        if(admin == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该管理员");
        }
        if(!adminDTO.getPassword().equals(admin.getPassword())){
            throw new ServiceException(ServiceCode.ERR_BAD_REPWD,"密码不一致！");
        }
        return admin;
    }

    /**
     * 获取管理员信息
     * @param name
     * @return
     */
    public Admin getAdmin(String name){
        Admin admin = adminMapper.getByName(name);
        if(admin == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该管理员");
        }
        return admin;
    }

    /**
     *
     * @param name
     * @param admin
     */
    public void edit(String name,Admin admin){
        Admin admin1 = adminMapper.getByName(name);
        if(admin1 == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该管理员");
        }
        admin1.setName(admin.getName());
        admin1.setPhone(admin.getPhone());
        admin1.setEmail(admin.getEmail());
        admin1.setAddress(admin.getAddress());
        int result = adminMapper.edit(admin1);
        if(result<0){
            throw new ServiceException(ServiceCode.ERR_FILE_UPLOAD,"更新失败！");
        }
    }

    /**
     *
     * @param name
     * @param OldPassword
     * @param NewPassword
     */
    public void updatePwd(String name,String OldPassword,String NewPassword){
        Admin admin = adminMapper.getByName(name);
        if(admin == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该管理员");
        }
        if(!admin.getPassword().equals(OldPassword)){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"旧密码不正确！");
        }
        admin.setPassword(NewPassword);
        int result = adminMapper.edit(admin);
        if(result<0){
            throw new ServiceException(ServiceCode.ERR_FILE_UPLOAD,"更新失败！");
        }
    }

    /**
     * 上传头像
     * @param name
     * @param image
     */
    public void avatar( String name, MultipartFile image) {
        String uploadDirectory = "D:/vue-program/Electronics-Mall/mall/public/image/avatar";

        try {
            if (image.isEmpty()) {
                throw new ServiceException(ServiceCode.ERR_BAD_REQUEST, "请选择一个非空文件。");
            }

            // 创建上传目录，如果不存在
            Path uploadPath = Paths.get(uploadDirectory);
            if (!Files.exists(uploadPath)) {
                Files.createDirectories(uploadPath);
            }

            // 文件存储
            String fileName = name + "_" + image.getOriginalFilename();
            Path targetPath = uploadPath.resolve(fileName);
            Files.copy(image.getInputStream(), targetPath, StandardCopyOption.REPLACE_EXISTING);

            // 保存地址
            Admin admin = adminMapper.getByName(name);
            admin.setAvatar(targetPath.toString());
            int result = adminMapper.edit(admin);

            if (result <= 0) {
                throw new ServiceException(ServiceCode.ERR_SAVE_FAILED, "保存失败！");
            }
        } catch (Exception e) {
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED, "文件上传失败 - " + e.getMessage());
        }
    }

    /**
     * 显示头像
     * @param name
     * @return
     */
    public String getAvatar(String name){
        Admin admin = adminMapper.getByName(name);
        if(admin == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该管理员");
        }
        return admin.getAvatar();
    }
}
